package com.example.beson.androiddemo.login.mvp.presenter;

import com.example.beson.androiddemo.login.mvp.BasePresenter;
import com.example.beson.androiddemo.login.mvp.model.UserInfo;
import com.example.beson.androiddemo.login.mvp.view.ILoginView;
import com.example.beson.androiddemo.utils.CheckedIsPhoneNumUtil;
import com.example.beson.androiddemo.utils.StringUtils;

/**
 * Created by 史国院 on 2017/4/25.
 * Desc 实现抽象方法以及处理逻辑
 */

public class LoginPresenter implements BasePresenter {
    private ILoginView mLoginView;

    public LoginPresenter(ILoginView mLoginView) {
        this.mLoginView = mLoginView;
    }

    @Override
    public void start() {

    }

    @Override
    public void finish() {
        //与 View 解绑
        if (mLoginView!=null){
            mLoginView = null;
        }
    }

    public boolean checkUserNameLogin(String userName, String password) {
        mLoginView.showLoading();
        if(StringUtils.isEmpty(userName)){
            mLoginView.phoneError("手机号不能为空！");
            mLoginView.dismissLoading();
            return false;
        }else{
            if(!CheckedIsPhoneNumUtil.checkphone(userName)){
                mLoginView.phoneError("手机号格式不正确！");
                mLoginView.dismissLoading();
                return false;
            }
        }
        if(StringUtils.isEmpty(password)){
            mLoginView.passwordError("密码不能为空！");
            mLoginView.dismissLoading();
            return false;
        }
        return true;
    }

    public void phoneLogin(String phone, String password) {
        UserInfo userInfo=new UserInfo();
        userInfo.setPhonenum(phone);
        userInfo.setPassword(password);
        if(phone.equals("18653091256")){
            mLoginView.loginSuccess(userInfo);
        }else{
            mLoginView.loginFailed(userInfo);
        }
        mLoginView.dismissLoading();
    }
}
